﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using ScrumTable.BL.DM.Properties;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Contains the basic information about an iteration.
    /// </summary>
    public class Iteration : DomainBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DomainCollection<UserStory> _stories;
        private DomainCollection<Bug> _bugs;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------


        /// <summary>
        /// Gets a list of all stories associated to the sprint (iteration) backlog.
        /// </summary>
        public DomainCollection<UserStory> Stories
        {
            get
            {
                if (_stories == null)
                {
                    _stories = new DomainCollection<UserStory>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.UserStorySchema]);
                }
                return _stories;
            }
        }

        /// <summary>
        /// Gets a list of all bugs.
        /// </summary>
        public DomainCollection<Bug> Bugs
        {
            get
            {
                if (_bugs == null)
                {
                    _bugs = new DomainCollection<Bug>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.BugSchema]);
                }
                return _bugs;
            }
        }

        /// <summary>
        /// Gets the name of the current domain object. In most cases this
        /// property will contain the domain object title.
        /// </summary>
        public override string Name
        {
            get { return DataObject.GetData(Schema.IterationSchema.Name, string.Empty); }
            set { }
        }

        /// <summary>
        /// Gets the iteration end date time.
        /// </summary>
        public DateTime EndTime
        {
            get { return DataObject.GetData<DateTime>(Schema.IterationSchema.EndTime); }
            set { SetDataNotNullWithUndoAction(Schema.IterationSchema.EndTime, value); }
        }

        /// <summary>
        /// Gets the iteration start date time.
        /// </summary>
        public DateTime StartTime
        {
            get { return DataObject.GetData<DateTime>(Schema.IterationSchema.StartTime); }
            set { SetDataNotNullWithUndoAction(Schema.IterationSchema.StartTime, value); }
        }

        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Parent.SearchCollection<Iteration>(); }
        }

        /// <summary>
        /// Gets a collection which contains all child collections of the current
        /// domain object.
        /// </summary>
        internal override ICollection ChildCollections { get { return new IDomainCollection[] { Stories, Bugs }; } }

        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.IterationSchema; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Resets all child collections (IDomainCollection instances) of the
        /// current domain object.
        /// </summary>
        protected override void ResetChildCollections()
        {
            _stories = null;
            _bugs = null;
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.IterationSchema.Name, "Name");
            PropertySchemaMappings.Add(Schema.IterationSchema.EndTime, "EndTime", true);
            PropertySchemaMappings.Add(Schema.IterationSchema.StartTime, "StartTime", true);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}